#!/bin/bash
# Special thank to Shiroko, he give me the insight of coding in proper way

################################# Here we import our tools


################################# Here we set the configs
REALPATH=`realpath $0`
HERE_PATH=`dirname $REALPATH`
STORE_APP_PATH="$HERE_PATH"
WORKDIR=`mktemp -d`
ARCH="" # Define it later
ARCH_ANOTHERWAY="" # Define it later

PKG_NAME="wemeet"
APP_NAME="腾讯会议(Linux)"
#SOURCE_LIST_FILE="/etc/apt/sources.list.d/wcbing.list"
GITHUB_REPO=""
VERSION="" #Here we use other way to get version so do not write here
TAGS="ubuntu;deepin;uos"
CATAGORY="chat" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="全球跨境视频会议变得更好！Wemeet打破国界，在全球100多个国家/地区提供流畅，安全和可靠的基于云的视频会议。轻松安排或加入具有清晰音频和高清视频质量的会议。通过即时消息、屏幕和文档共享等方式，在全球团队之间有效协作！" # This should be written as json format
MAINTAINER="shenmo<shenmo@spark-app.store>"
HOMEPAGE="https://meeting.tencent.com/"


################################# Fill the variables below only if you use write_control function

SECTION=""
PRIORITY=""
DEPENDS=""
DESCRIPTION_SHORT=""
DESCRIPTION_LONG=""
PROVIDES=""
REPLACES=""
RECOMMENDS=""

source /opt/bashimport/sbs-utils.bashimport
source /opt/bashimport/sbs-build-helper.bashimport

if [ "$1" = "amd64" ] || [ "$1" = "x64" ];then
ARCH="amd64"
ARCH_ANOTHERWAY="-amd64"
ARCH_FOR_LOONG="amd64"
PLATFORM_NUM="0"
elif [ "$1" = "arm64" ] || [ "$1" = "arm" ];then
ARCH="arm64"
ARCH_ANOTHERWAY="-aarch64"
ARCH_FOR_LOONG="arm64"
PLATFORM_NUM="1"
elif [ "$1" = "loong64" ];then
ARCH="loong64"
ARCH_ANOTHERWAY="-loong64"
ARCH_FOR_LOONG="loongarch64"
PLATFORM_NUM="2"
else
log.error "Err:Invalid Architecture"
exit 1
fi


function prepare(){
    aptss_get_update
    
}




function build(){

pushd $STORE_APP_PATH

#curl -fs https://im.qq.com/rainbow/linuxQQDownload | sed -nE 's/.*var params\s?=\s?(\{.*\});.*/\1/p' | jq -r '.version, .x64DownloadUrl.deb, .armDownloadUrl.deb, .loongarchDownloadUrl.deb'

#download_list="$(curl -fs https://im.qq.com/rainbow/linuxQQDownload | sed -nE 's/.*var params\s?=\s?(\{.*\});.*/\1/p')"
#wget $(echo $download_list  | jq -r ".${ARCH_FOR_QQ}DownloadUrl.deb") -O QQ.deb
download_list_url="https://meeting.tencent.com/web-service/query-download-info?q=%5B\
%7B%22package-type%22%3A%22app%22%2C%22channel%22%3A%220300000000%22%2C%22platform%22%3A%22linux%22%2C%22arch%22%3A%22x86_64%22%2C%22decorators%22%3A%5B%22deb%22%5D%7D%2C\
%7B%22package-type%22%3A%22app%22%2C%22channel%22%3A%220300000000%22%2C%22platform%22%3A%22linux%22%2C%22arch%22%3A%22arm64%22%2C%22decorators%22%3A%5B%22deb%22%5D%7D%2C\
%7B%22package-type%22%3A%22app%22%2C%22channel%22%3A%220300000000%22%2C%22platform%22%3A%22linux%22%2C%22arch%22%3A%22loongarch64%22%2C%22decorators%22%3A%5B%22deb%22%5D%7D\
%5D&nonce=D38mxpWk6TMTA44a"


JSON=$(curl -s $download_list_url)

PKG_VER_UPSTREAM_amd64=$(echo $JSON | jq -r '."info-list"[0].version')

PKG_VER_UPSTREAM_arm64=$(echo $JSON | jq -r '."info-list"[1].version')

PKG_VER_UPSTREAM_loong64=$(echo $JSON | jq -r '."info-list"[2].version')

eval "PKG_VER_UPSTREAM=\$PKG_VER_UPSTREAM_${ARCH}"

download_link_amd64="$(echo $JSON | jq -r '."info-list"[0].url')"

download_link_arm64="$(echo $JSON | jq -r '."info-list"[1].url')"

download_link_loong64="$(echo $JSON | jq -r '."info-list"[2].url')"

eval "download_link=\$download_link_${ARCH}"

PKG_VER_APTSS=`get_version_from_source_list_file ${PKG_NAME}:${ARCH} /opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d/sparkstore${ARCH_ANOTHERWAY}.list`
    #PKG_VER_UPSTREAM=`get_version_from_source_list_file ${PKG_NAME}:${ARCH} ${SOURCE_LIST_FILE}`

    
    
    #PKG_VER_UPSTREAM=$(echo "$download_list" | jq -r '.version')



    compare_version ${PKG_VER_APTSS} ${PKG_VER_UPSTREAM}
    # Compare version will also give the value to $VERSION
    # Now we should have the $VERSION 

   if [ -e $VERSION ];then
    log.error "VERSION is empty. This should not happen! exit."
    fi

aria2c "${download_link}" -o TEMP.deb

VERSION=$(dpkg --field ./TEMP.deb version)
if [ "${ARCH}" = "loong64" ];then
./repack-loongarch-to-loong.sh ./TEMP.deb
#https://gitee.com/amber-ce/ace-host-package-creator/blob/master/repack-loongarch-to-loong.sh
else
mv -v ./TEMP.deb ${PKG_NAME}_${VERSION}_${ARCH}.deb
fi
 
popd

}


prepare
build
write_json $STORE_APP_PATH

rm -rf $WORKDIR
